//
//  YDHTextView.m
//  Ashton
//
//  Created by pengpeng on 2023/6/17.
//  Copyright © 2023 Falcon Automation. All rights reserved.
//

#import "YDHTextView.h"
#import <Masonry/Masonry.h>

@interface YDHTextView()<UITextViewDelegate>

@property (nonatomic, strong) UILabel *plaleLabel;
@property (nonatomic, strong) UILabel *countLabel;

@end

@implementation YDHTextView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupPlaceholder:nil placeholderColor:nil];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
                  placeholder:(NSString *)placeholder
             placeholderColor:(UIColor *)placeholderColor {
    if (self = [super initWithFrame:frame]) {
        self.limitWords = 99999;
        [self setupPlaceholder:nil placeholderColor:nil];
        self.placeholderGlobal = placeholder;
        self.placeholderColorGlobal = placeholderColor;
    }
    return self;
}

- (void)setPlaceholderGlobal:(NSString *)placeholderGlobal {
    _placeholderGlobal = placeholderGlobal;
    [self refreshPlaleLabelText:placeholderGlobal];
}

- (void)setPlaceholderColorGlobal:(UIColor *)placeholderColorGlobal {
    _placeholderColorGlobal = placeholderColorGlobal;
    self.plaleLabel.textColor = placeholderColorGlobal;
}

- (void)setIsShowCountLabel:(BOOL)isShowCountLabel {
    _isShowCountLabel = isShowCountLabel;
    self.countLabel.hidden = !isShowCountLabel;
}

@synthesize text = _text;
- (NSString *)text {
    return self.palceholdertextView.text;
}
- (void)setText:(NSString *)text {
    self.palceholdertextView.text = text;
    [self checkShowHiddenPlaceholder];
    [self refreshCountLabel];
}

- (void)setLimitWords:(NSInteger)limitWords {
    _limitWords = limitWords;
    [self refreshCountLabel];
}

- (void)refreshPlaleLabelText:(NSString *)string {
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.placeholderGlobal ?: @""];
    paragraphStyle.lineSpacing = 15;
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, self.placeholderGlobal.length)];
    self.plaleLabel.attributedText = attributedString;
}

- (void)checkShowHiddenPlaceholder {
    if (self.palceholdertextView.hasText) {
        self.plaleLabel.text = nil;
        self.countLabel.hidden = !self.isShowCountLabel;
    } else {
        [self refreshPlaleLabelText:self.placeholderGlobal];
        self.countLabel.hidden = !self.isShowCountLabel;
    }
}

- (void)refreshCountLabel {
    NSString *string1 = [NSString stringWithFormat:@"%ld", self.palceholdertextView.text.length];
    NSString *string2 = [NSString stringWithFormat:@"/%ld", self.limitWords];
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@", string1, string2] attributes:@{
        NSFontAttributeName: [UIFont systemFontOfSize:12],
        NSForegroundColorAttributeName: rgba(153, 153, 153, 1)
    }];
    self.countLabel.attributedText = attrString;
    [self.countLabel sizeToFit];
}

- (void)setupPlaceholder:(NSString *)placeholder placeholderColor:(UIColor *)placeholderColor {
    self.palceholdertextView = [[UITextView alloc] init];
    self.palceholdertextView.delegate = self;
    self.palceholdertextView.frame = CGRectMake(2, 2, self.gk_width - 4, self.gk_height - 30);
    [self addSubview:self.palceholdertextView];

    self.palceholdertextView.font = [UIFont systemFontOfSize:15];
    [self.palceholdertextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(2);
        make.bottom.mas_equalTo(-30);
        make.left.mas_equalTo(2);
        make.right.mas_equalTo(-2);
    }];

    self.palceholdertextView.typingAttributes = @{
        NSFontAttributeName: [UIFont systemFontOfSize:15]
    };

    self.plaleLabel = [[UILabel alloc] init];
    self.plaleLabel.textColor = placeholderColor;
    self.plaleLabel.textAlignment = NSTextAlignmentLeft;
    self.plaleLabel.font = self.palceholdertextView.font;
    self.plaleLabel.numberOfLines = 0;
    self.placeholderGlobal = placeholder;
    [self addSubview:self.plaleLabel];

    [self.plaleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(8);
        make.left.mas_equalTo(8);
        make.right.mas_equalTo(-8);
    }];

    UILabel *countLabel = [[UILabel alloc] init];
    self.countLabel = countLabel;
    self.countLabel.font = self.palceholdertextView.font;
    self.countLabel.textColor = placeholderColor;
    [self addSubview:self.countLabel];
    [countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-10);
        make.bottom.mas_equalTo(-5);
    }];
}

#pragma mark delegate

- (void)textViewDidChange:(UITextView *)textView {

    if (textView.text.length >= self.limitWords) {
        textView.text = [textView.text substringToIndex:self.limitWords];
    }
    [self checkShowHiddenPlaceholder];

    [self refreshCountLabel];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"] && self.isReturnHidden) {
        [textView resignFirstResponder];
    }

    //大于等于限制字数，而且不是删除键的时候不可以输入。
    //考虑复制粘贴和光标不在最后得情况
    //        if textView.text.count + range.length + text.count > limitWords && !(text as NSString).isEqual(to: ""){
    //            return false
    //        }

    return YES;
}

@end
